﻿using HandyControl.Tools.Command;
using Stylet;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Controls;
using ImgConventICO.Models;
using System.Collections.ObjectModel;

using System.Windows;
using HandyControl.Data;
using HandyControl.Controls;
using System;
using HandyControl.Tools.Extension;
using System.Collections;

namespace ImgConventICO.ViewModels
{
    public class RootViewModel : PropertyChangedBase
    {
        private string _title = "HandyControl Application";
        private DataInfoModel _selectedItem;
        private ObservableCollection<DataInfoModel> _items;
        private bool _isPopupOpen;
        private string _selectedItemContent;
        private string _info;



        public DataInfoModel SelectedItem
        {
            get => _selectedItem;
            set
            {
                SetAndNotify(ref _selectedItem, value);
                // HandyControl.Controls.MessageBox.Show("You selected " + value.Name, "Selected Item",MessageBoxButton.OK,MessageBoxImage.Information);
                

                   SelectedItemContent = SelectedItem.Icon ;
                
            }
        }
        public string Title
        {
            get { return _title; }
            set { SetAndNotify(ref _title, value); }
        }


        public ObservableCollection<DataInfoModel> Items 
        {
            get { return _items; }
            set { SetAndNotify(ref _items, value); 
            
            
            }

        }
        

        public bool IsPopupOpen
        {
            get { return _isPopupOpen; }
            set { SetAndNotify(ref _isPopupOpen, value); }
        }

        public string SelectedItemContent
        {
            get { return _selectedItemContent; }
            set { SetAndNotify(ref _selectedItemContent, value); }
        }

        public string Info
        {
            get { return _info; }
            set { SetAndNotify(ref _info, value); }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public RootViewModel()
        {
            Items = new ObservableCollection<DataInfoModel>();
            new RelayCommand(ListView_MouseDoubleClick);

        }


        public RelayCommand OpenCommand => new RelayCommand(Status);
        public RelayCommand OpenConfigCommand => new RelayCommand(OpenPopup=> { if (!SelectedItemContent.IsNullOrEmpty()) IsPopupOpen = true; });
        public RelayCommand IconShowCommand => new RelayCommand(iconshow=> {//有重复,待优化
            //var ifc = IconFontConstModel.hashtable;
            //foreach (DictionaryEntry item in ifc)
            //{
            //    SelectedItemContent = (item.Key + " : " + item.Value);
            //    System.Threading.Tasks.Task.Delay(3000);
            //}
            //todo:优化

        });
        public RelayCommand ListView_MouseDoubleCommand { get; }

        private void ListView_MouseDoubleClick(object obj)
        {
            if (!(obj is string name)) return;
            
            Info = "You double clicked on Id: "+name;
        }


        /// <summary>
        /// 打开弹窗
        /// </summary>
        /// <param name="parameter"></param>
        private void Status(object parameter)
        {
            //TODO: Show status message
            if (!(parameter is Button btn)) return; 
            
            btn.Content = btn.Content.ToString() == "Clicked again" ? "Clicked" : "Clicked again";

            for (int i = 0; i < 310; i++)
            {

                Items.Add(new DataInfoModel() { Id = i, Name = IconFontModel.GetIconFontName((IconFontModel.IconType)i + 1)
                    + "       代码:  " + 
                    IconFontModel.GetIconFontCode((IconFontModel.IconType)i + 1) ,
                    Icon = IconFontModel.GetIconFont((IconFontModel.IconType)i+1) });

                // ConvertToIcon("E:\\MyDesktopFile\\ICON\\ICON-PNG\\wgm.png", "E:\\MyDesktopFile\\ICON\\ICON-ICO\\wgm2.ico");
            }
            


        }

        /// <summary>
        /// 将图片转换为ICO格式
        /// </summary>
        /// <param name="sourceImagePath"></param>
        /// <param name="destinationIconPath"></param>
        public void ConvertToIcon(string sourceImagePath, string destinationIconPath)
        {
            //var _img = new Bitmap(sourceImagePath);
            //// 调整图片大小
            //using (_img = new Bitmap(_img, new Size(128, 128)))
            //{
            //    // 将Bitmap保存为ICO格式
            //    _img.Save(destinationIconPath, ImageFormat.Icon);
            //}

            // 加载源图像
            using (var image = System.Drawing.Image.FromFile(sourceImagePath))
            {
                // 调整图像大小
                var img = image.GetThumbnailImage(128, 128, null, System.IntPtr.Zero);
                // 保存为指定格式
                img.Save(destinationIconPath, ImageFormat.Icon);
            }
        }
    }
}
